import { _decorator, Component, UITransform, v3 } from 'cc';
const { ccclass, property, executionOrder } = _decorator;

import DragTetris_CardFunctions from "../Common/DragTetris_CardFunctions";
import DragTetris_GameConfig from "../Data/DragTetris_GameConfig";
import { DragTetris_GameData } from "../Data/DragTetris_GameData";
import DragTetris_GameManager from "./DragTetris_GameManager";
import DragTetris_Grid from "../Game/DragTetris_Grid";
import { DragTetris_GameStorage } from "../Data/DragTetris_GameStorage";
import DragTetris_Block from "../Game/DragTetris_Block";
// // 慢一点执行

@ccclass('DragTetrisMapManager')
@executionOrder(102)
export default class DragTetris_MapManager extends Component {
    static Instance: DragTetris_MapManager;
    onLoad() {
        DragTetris_MapManager.Instance = this;
    }
    initCard() {
        this.node.destroyAllChildren();
        // DragTetris_GameData.arrCards = [];
        DragTetris_GameData.arrGrids = [];
        this.node.getComponent(UITransform).width = DragTetris_GameConfig.W + 0; // this.cardWidth * this.column + 20;
        this.node.getComponent(UITransform).height = DragTetris_GameConfig.H + 0; // this.cardHeight * this.row + 20;

        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            DragTetris_GameData.arrGrids[x] = [];
            // GameData.arrCards[x] = [];
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                // const id = testData[x][y];
                const id = 0;
                const scrGrid: DragTetris_Grid = DragTetris_CardFunctions.CreateGrid(id, x, y);
                DragTetris_GameData.arrGrids[x][y] = scrGrid;

            }
        }

        this.InitCreateBlockGroup();

        DragTetris_GameManager.NewGame();
    }
    InitCreateBlockGroup() {
        // console.log(`DragTetris_GameStorage.data.map111`, DragTetris_GameStorage.data.map);

        // if (DragTetris_GameStorage.data.map.length > 0) {
        if (DragTetris_GameStorage.data.map.isEnd) {
            DragTetris_GameStorage.InitMap();

            for (let index = 0; index < 3; index++) {
                DragTetris_CardFunctions.随机创建一个BlockGroup(index);
            }

            DragTetris_GameData.SaveData();


            DragTetris_GameStorage.data.map.isEnd = false;
            DragTetris_GameStorage.saveData();
        }
        else {
            this.Init_arrCardGroups_temp();
            this.Init_Blocks();
        }
    }
    Init_arrCardGroups_temp() {
        for (let index = 0; index < DragTetris_GameStorage.data.map.arrCardGroups_temp.length; index++) {
            let item_data = DragTetris_GameStorage.data.map.arrCardGroups_temp[index];
            // console.log(`item_data`, item_data);

            DragTetris_CardFunctions.CreateBlockGroup(item_data.id, item_data.x, item_data.y, item_data.color, item_data.posType);
        }
    }
    Init_Blocks() {
        if (DragTetris_GameStorage.data.map.blocks.length > 0) {
            for (let index = 0; index < DragTetris_GameStorage.data.map.blocks.length; index++) {
                let item_data = DragTetris_GameStorage.data.map.blocks[index];
                // console.log(`item_data`, item_data);

                let block: DragTetris_Block = DragTetris_CardFunctions.CreateBlock(item_data.posType, item_data.x, item_data.y, item_data.point, item_data.color);
                let grid: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(item_data.x, item_data.y)
                grid.block = block;
                block.grid = grid;
                block.node.parent = block.grid.node;
                block.node.position = v3(0, 0, 0);
            }
        }
    }
}

// /** 调试 */
window["DragTetris_MapManager"] = DragTetris_MapManager;